<%@ page contentType="text/html;charset=utf-8" language="java"
	pageEncoding="UTF-8" import="java.util.Date"%>
<!DOCTYPE html>
<html lang="en">
  <head>
     <%@include file="/WEB-INF/jsp/common/style.jsp"%>
  </head>
  <body>
    <div id="wrap">
    <input type="hidden" id="projectId" value="${model.project.id}"> 
    <input type="hidden" id="userIds" value="${model.project.projectUserIds}"> 
   <div class="main_container" id="forms_page">
     	<div class="row-fluid">
          <div class="widget widget-padding span12">          
          
              <div class="widget-forms clearfix"  style="padding-top:15px">
              	 <form class="form-horizontal" id="content">
              	 
              	  <div class="control-group">
                    <label class="control-label" ><font color="red">*</font>项目名称</label>
                    <div class="controls">
                      <input name="projectName" id="projectName"  maxlength="100"   type="text" class="w300"  value="${model.project.projectName}"   />                      
                    </div>
                  </div>
                 
					
                  
                                                                  
                    </form>
                  </div>
				
				 <select id="projectUserSelect"  name="projectUserSelect"   type="select"  style="display:none">
					   		<option value="" >请选择</option>
					   		<c:forEach items="${model.users}" var="lo" varStatus="itemStatus">
					               <option value="${lo.id}" >${lo.real_name}</option>
					        </c:forEach>  
				    </select>

            </div>        
          </div>
          	<div class="row-fluid">
          		<div class="footbtn"> 
          		<button class="btn btn-primary" type="submit"  onclick="save()">保存</button>
               <button class="btn" type="button" onclick="backIndex()">取消</button>
               </div>
          	</div>
          </div>
      </div>    
  </body>
   	   <script type="text/javascript">
           		var num=0;
           		$(document).ready(function(){
            		var userIds=$('#userIds').val();
            		if(userIds==''){
            			addUser(0);
            		}else{
            			var ids= userIds.split(',');
            		 	for(var i=0;i<ids.length;i++){    
            	      		var userId= parseInt(ids[i])   
            	      		addUser(userId);
            	      	}

            		}
           		});
           		function addUser(userId){
           			var user=$('#projectUserSelect').clone(true);
           			num++;
           			//user.attr("id","select_" + num);
           			
           			var op = "<div class=\"control-group\" id='div_"+ num +"'>";
           			op =op+  " <label class=\"control-label\" name='projecLabletName' >项目成员"+ num +":</label>";
           			op =op+  "  <div class=\"controls\">";
           			op =op+  "<select id='select_" + num + "'  class=\"w300\" name=\"projectUserSelect\"   type=\"select\" >";
           			op=op + user.html();
           			op =op+  "</select>";
           			if(num==1){
           				op=op +  "<a   name='btnadd'  href=\"javascript:addUser(0)\"   style=\"margin-left: 5px\"  >新增</a>";
           			}else{
           				op=op +  "<a   name='btnadd'   href=\"javascript:del("+num+")\"  style=\"margin-left: 5px\"  >删除</a>";
           			}
           			op=op + "</div>";
                    $("#content").append(op);
                    $("#select_" + num).val(userId);
           			
           		}
           		function del(id){
           			$('#div_' + id).remove();
           			//重新计算
           			num=0;
           			$("#content div label").each(function(){	
           				var name=$(this).attr("name");
           				if(name!=undefined && name=='projecLabletName'){
           					num++;
           					$(this).html("项目成员" + num + ":");
           				}
           				
           			});
           		}
  </script>

  <script type="text/javascript">
  function save(){
		var postData="projectId=" +$('#projectId').val() ;
		
		var projectName=$('#projectName').val();
		if(projectName==''){
			layer.alert('请输入项目名称');
	    	return;
		}
		postData=postData + "&projectName=" + encodeURIComponent(projectName);
		
		var flag=false;
	    //alert($("#content div div select").html());
		$("#content div div select").each(function(){	
			//alert($(this).html());
			var val=$(this).val();
			if(val==""){
						
			}else{
				postData=postData + "&projectUserIds=" + val;
			}
		});
		if(flag){
			return;
		}
		//projectUserSelect
		
	
	    layer.load(1, {shade: [0.5,'#fff']});
	   //alert(postData);
	    $.ajax({
	        type: "POST",
	        url: "save",
	        data: postData,
	        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
	        async: true, // 同步  true 异步。默认是异步
	        dataType: 'json',
	        error: function(resultdata) {
	        	layer.closeAll();
	        },
	        success: function(resultdata) {
	        	layer.closeAll();
	            if(resultdata.returnCode=="0000"){               	
	            	layer.alert('操作成功',{icon: 1}, function(){
	            	 	 parent.editReturn();
	            		//layer.closeAll(); 
	            		 //layer.closeAll();
	            		//window.location.href="${pageContext.request.contextPath}/distrbutor/inventory/index";
	            	});
	            }else{
	            	layer.alert(resultdata.returnMessage);
	            }
	        }
	    });
	}
  function backIndex(){	  
 	 parent.editReturn();
  }
	</script>
</html>
    